package top.yangbuyi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import top.yangbuyi.constants.Constants;
import top.yangbuyi.dto.RegistrationDto;
import top.yangbuyi.mapper.RegistrationMapper;
import top.yangbuyi.domain.Registration;
import top.yangbuyi.service.RegistrationService;
import top.yangbuyi.utils.StringUtils;
import top.yangbuyi.vo.DataGridView;

@Service
public class RegistrationServiceImpl extends ServiceImpl<RegistrationMapper, Registration> implements RegistrationService{

    @Autowired
    private RegistrationMapper registrationMapper;

	@Override
	public DataGridView queryRegistrationForPage(RegistrationDto registrationDto) {
		Page<Registration> page = new Page<>(registrationDto.getPageNum(), registrationDto.getPageSize());
		QueryWrapper<Registration> qw = new QueryWrapper<>();
		qw.eq(registrationDto.getDeptId() != null, Registration.COL_DEPT_ID, registrationDto.getDeptId());
		qw.like(StringUtils.isNotBlank(registrationDto.getPatientName()), Registration.COL_PATIENT_NAME, registrationDto.getPatientName());
		qw.eq(StringUtils.isNotBlank(registrationDto.getSchedulingType()), Registration.COL_SCHEDULING_TYPE, registrationDto.getSchedulingType());
		qw.eq(StringUtils.isNotBlank(registrationDto.getSubsectionType()), Registration.COL_SUBSECTION_TYPE, registrationDto.getSubsectionType());
		qw.eq(StringUtils.isNotBlank(registrationDto.getRegStatus()), Registration.COL_REG_STATUS, registrationDto.getRegStatus());
		qw.eq(StringUtils.isNotBlank(registrationDto.getVisitDate()), Registration.COL_VISIT_DATE, registrationDto.getVisitDate());
		qw.orderByDesc(Registration.COL_CREATE_TIME);
		this.registrationMapper.selectPage(page, qw);
		return new DataGridView(page.getTotal(), page.getRecords());
	}

//	@Override
//	public Registration queryRegistrationByRegId(String registrationId) {
//		return this.registrationMapper.selectById(registrationId);
//	}

//	@Override
//	public int updateRegistrationById(Registration registration) {
//		return this.registrationMapper.updateById(registration);
//	}

	@Override
	public List<Registration> queryRegistration(Long deptId, String subsectionType, String scheudlingType, String regStatus, Long userId) {
		QueryWrapper<Registration> wrapper = new QueryWrapper<>();
		wrapper.eq(Registration.COL_DEPT_ID, deptId);
		wrapper.eq(StringUtils.isNotBlank(subsectionType),Registration.COL_SUBSECTION_TYPE, subsectionType);
		wrapper.eq(Registration.COL_SCHEDULING_TYPE, scheudlingType);
		wrapper.eq(Registration.COL_REG_STATUS, regStatus);
		// 解决查询挂号日期Bug
		wrapper.eq(Registration.COL_VISIT_DATE,DateUtil.format(DateUtil.date(),"yyyy-MM-dd"));
		wrapper.eq(null != userId, Registration.COL_USER_ID, userId);
		wrapper.orderByAsc(Registration.COL_REG_NUMBER);
		return this.registrationMapper.selectList(wrapper);
	}
//    @Override
//    public DataGridView queryRegistrationForPage(RegistrationDto registrationDto) {
//        Page<Registration> page=new Page<Registration>(registrationDto.getPageNum(),registrationDto.getPageSize());
//        QueryWrapper<Registration> qw=new QueryWrapper<>();
//        qw.eq(registrationDto.getDeptId()!=null,Registration.COL_DEPT_ID,registrationDto.getDeptId());
//        qw.like(StringUtils.isNotBlank(registrationDto.getPatientName()),Registration.COL_PATIENT_NAME,registrationDto.getPatientName());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getSchedulingType()),Registration.COL_SCHEDULING_TYPE,registrationDto.getSchedulingType());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getSubsectionType()),Registration.COL_SUBSECTION_TYPE,registrationDto.getSubsectionType());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getRegStatus()),Registration.COL_REG_STATUS,registrationDto.getRegStatus());
//        qw.eq(StringUtils.isNotBlank(registrationDto.getVisitDate()),Registration.COL_VISIT_DATE,registrationDto.getVisitDate());
//        qw.orderByDesc(Registration.COL_CREATE_TIME);
//        this.registrationMapper.selectPage(page,qw);
//        return new DataGridView(page.getTotal(),page.getRecords());
//    }

    /**
     * 根据患者id查询患者信息
     * @param regId
     * @return
     */
    @Override
    public Registration queryRegistrationByRegId(String regId) {
        return this.registrationMapper.selectById(regId);
    }

    /**
     * 修改患者信息
     * @param registration
     * @return
     */
    @Override
    public int updateRegistrationById(Registration registration) {
        return this.registrationMapper.updateById(registration);
    }

    @Override
    public void addRegistration(RegistrationDto registrationDto) {
        Registration registration=new Registration();
        BeanUtil.copyProperties(registrationDto,registration);
        registration.setRegStatus(Constants.REG_STATUS_0);
        registration.setCreateBy(registrationDto.getSimpleUser().getUserName());
        registration.setCreateTime(DateUtil.date());
        this.registrationMapper.insert(registration);
    }
}
